package IGU;

import javax.swing.JFrame;
import logica.Comunicacion;
import logica.FuncionesGUI;

public class LoginImovie extends javax.swing.JFrame {
    private boolean validate;
    private boolean esUsuario;


    /** Creates new form AcercaDe */
    public LoginImovie() {
        initComponents();
        initMyComponets();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        MainAcceder = new javax.swing.JButton();
        jNick = new javax.swing.JTextField();
        jPassword = new javax.swing.JPasswordField();
        MainClave = new javax.swing.JLabel();
        MainNickname = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("iMovie");
        jLabel1.setName("jLabel1"); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(20, 20, 58, 22);

        jPanel1.setBackground(new java.awt.Color(51, 51, 51));
        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(null);

        MainAcceder.setText("Ingresar");
        MainAcceder.setName("MainAcceder"); // NOI18N
        MainAcceder.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                MainAccederActionPerformed(evt);
            }
        });
        jPanel1.add(MainAcceder);
        MainAcceder.setBounds(240, 340, 90, 23);

        jNick.setName("jNick"); // NOI18N
        jPanel1.add(jNick);
        jNick.setBounds(180, 180, 151, 20);

        jPassword.setName("jPassword"); // NOI18N
        jPanel1.add(jPassword);
        jPassword.setBounds(180, 240, 150, 20);

        MainClave.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        MainClave.setForeground(new java.awt.Color(255, 255, 255));
        MainClave.setText("Password:");
        MainClave.setName("MainClave"); // NOI18N
        jPanel1.add(MainClave);
        MainClave.setBounds(100, 250, 80, 14);

        MainNickname.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        MainNickname.setForeground(new java.awt.Color(255, 255, 255));
        MainNickname.setText("Nickname:");
        MainNickname.setName("MainNickname"); // NOI18N
        jPanel1.add(MainNickname);
        MainNickname.setBounds(100, 180, 70, 24);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IGU/imagen/claquetaImovie.png"))); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N
        jPanel1.add(jLabel4);
        jLabel4.setBounds(0, 130, 240, 270);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 9)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Server Aplication");
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel1.add(jLabel2);
        jLabel2.setBounds(30, 50, 100, 11);

        getContentPane().add(jPanel1);
        jPanel1.setBounds(-10, -10, 380, 390);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-373)/2, (screenSize.height-416)/2, 373, 416);
    }// </editor-fold>//GEN-END:initComponents
    private JFrame ventanaPadre; 
    public JFrame getVentanaPadre()
    {
        return ventanaPadre;
    }
    public void setVentanaPadre(JFrame ventanaPadre)
    {
        this.ventanaPadre = ventanaPadre;
    }



    private void MainAccederActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_MainAccederActionPerformed
if(FuncionesGUI.esUsuario(jNick,jPassword)==false)
{
 //se dispara una ventana de error (si estan vacios los campos, o la persona no es usuario)
         WarningAdd warning = new WarningAdd();

        FuncionesGUI.habilitarVentana(this, false, true);
        FuncionesGUI.habilitarVentana(warning, true, true);
        FuncionesGUI.ventanaSiempreEnTope(warning, true);
        warning.setVentanaPadre(this);
}
 else
{
     validate = FuncionesGUI.esAdmin(jNick, jPassword);
        if (validate==true){
         WindowMainAdmin admin = new WindowMainAdmin();

        FuncionesGUI.habilitarVentana(this, false, true);
        FuncionesGUI.habilitarVentana(admin, true, true);
        FuncionesGUI.ventanaSiempreEnTope(admin, true);
        admin.setVentanaPadre(this);
        this.dispose();
}
 else
 {
             WindowMainEmpleado empleado = new WindowMainEmpleado();

        FuncionesGUI.habilitarVentana(this, false, true);
        FuncionesGUI.habilitarVentana(empleado, true, true);
        FuncionesGUI.ventanaSiempreEnTope(empleado, true);
        empleado.setVentanaPadre(this);
        this.dispose();
 }

}

        // TODO add your handling code here:
    }//GEN-LAST:event_MainAccederActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginImovie().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton MainAcceder;
    private javax.swing.JLabel MainClave;
    private javax.swing.JLabel MainNickname;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jNick;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPassword;
    // End of variables declaration//GEN-END:variables

    private void initMyComponets() {
      jNick.setText("");
      jPassword.setText("");
      Comunicacion.inicializarComunicacion();
      Comunicacion.imprimirDatos();
    }

}
